package com.swpu.service_weight.controller;


import com.swpu.commonutils.JwtUtils;
import com.swpu.commonutils.R;
import com.swpu.commonutils.ResultEncryption;
import com.swpu.service_user.aop.Decrypt;
import com.swpu.service_user.aop.Encrypt;
import com.swpu.service_weight.entity.WeightRecord;
import com.swpu.service_weight.entity.vo.MonthDataVo;
import com.swpu.service_weight.entity.vo.WeightQueryVo;
import com.swpu.service_weight.entity.vo.WeightRecordVo;
import com.swpu.service_weight.service.WeightRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chaichai
 * @since 2024-02-01
 */
@RestController
@RequestMapping("/service_weight/weight-record")
public class WeightRecordController {
    @Autowired
    private WeightRecordService weightRecordService;
    @PostMapping("/recordWeight")
    @Decrypt
    @Encrypt
    public R recordUserWeight(@RequestBody ResultEncryption resultEncryption, WeightRecordVo weightRecordVo,HttpServletRequest request ) {
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()) return R.error();
        weightRecordService.recordUserWeight(openid, weightRecordVo);
        return R.ok();
    }
    @PostMapping("/getWeightMonthAvgData")
    @Decrypt
    @Encrypt
    public R getWeightMonthAvgData(@RequestBody ResultEncryption resultEncryption,  WeightQueryVo weightQueryVo,HttpServletRequest request) {
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()) return R.error();
        List<MonthDataVo> userWeightMonthData = weightRecordService.getUserWeightMonthData(openid, weightQueryVo);
        return R.ok().data("monthDayAvgData", userWeightMonthData);
    }

    @PostMapping("/getWeightMonthAllData")
    @Decrypt
    @Encrypt
    public R getWeightMonthAllData(@RequestBody ResultEncryption resultEncryption, WeightQueryVo weightQueryVo,HttpServletRequest request) {
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()) return R.error();
        List<WeightRecord> weightMonthAllData = weightRecordService.getWeightMonthAllData(openid, weightQueryVo);
        return R.ok().data("weightMonthAllData",weightMonthAllData);
    }
    @GetMapping("/getWeightDataToChart")
    public R getWeightDataToChart(HttpServletRequest request,@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "7") int pageSize){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户无访问权限");
        }
        List<WeightRecord> weightDataChart = weightRecordService.getWeightDataToChart(openid, page, pageSize);
        return R.ok().data("weightChartData",weightDataChart);
    }
    @GetMapping("/getUserLatestWeight")
    public R getUserLatestWeight(HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户未登录");
        }
        Double latestWeight = weightRecordService.getLatestWeightData(openid);
        return R.ok().data("latestWeight",latestWeight);
    }

}

